import os
import win32security
import win32net

# 设置NAS路径和用户凭证
#share_root = '\\10.70.222.232\Public'
#share_root = r'\\10.70.222.232\Public'
#hare_root = r'\\10.70.222.232\Public\华星大数据分析\大模型攻关会\日会\20250411\AI大模型周会进展_20250411.pptx'

# 文件路径（UNC 路径）
share_root = r"\\10.70.222.232\Public\华星大数据分析\大模型攻关会\日会\20250411\AI大模型周会进展_20250411.pptx"

username = "public"
password = "tclpublic1"

# 进行网络身份验证
def authenticate_network(username, password, domain=None):
    print("用户名："+username)
    print("密码："+password)
    try:
        win32net.NetUseAdd(None, 2, {'remote': share_root, 'password': password, 'username': username, 'domain': domain})
        print("Network authentication successful.")
    except Exception as e:
        print(f"Network authentication failed: {e}")

# 获取文件的安全描述符
def get_file_security_info(file_path):
    try:
        # 获取文件的安全描述符
        sd = win32security.GetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION | win32security.GROUP_SECURITY_INFORMATION)

        # 获取文件所有者
        owner_sid = sd.GetSecurityDescriptorOwner()
        try:
            owner_name, domain, _ = win32security.LookupAccountSid(None, owner_sid)
        except win32security.error as e:
            owner_name = f"Error: {e}"

        # 获取文件所属的组
        group_sid = sd.GetSecurityDescriptorGroup()
        try:
            group_name, domain, _ = win32security.LookupAccountSid(None, group_sid)
        except win32security.error as e:
            group_name = f"Error: {e}"

        return owner_name, group_name
    except Exception as e:
        return f"Error: {e}"


# def disconnect_existing_connections():
#     try:
#         os.system('net use \\10.70.222.232\Public /delete')
#         print("Disconnected from the network share.")
#     except Exception as e:
#         print(f"Error disconnecting: {e}")


def main():

    #disconnect_existing_connections()


    # 进行网络身份验证
    authenticate_network(username, password)

    # 调用获取文件安全信息的函数
    result = get_file_security_info(share_root)

    # 输出结果并检查返回值
    if isinstance(result, tuple) and len(result) == 2:
        owner, group = result
        print(f"Owner: {owner}")
        print(f"Group: {group}")
    else:
        print("Error occurred or unexpected return value:", result)

if __name__ == "__main__":
    main()
